// 登录一：首页打开Scratch时，自动获取一次用户登录信息
module.exports.requestSession = (resolve, reject) => (
    miniFetch(resolve, reject, '/user/getSession')
);

// 登录二：提交账号、密码进行登录
module.exports.requestLogin = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/user/login', {body:data})
});

// 退出：提交账号，退出登录状态
module.exports.requestLogout = (resolve, reject, data) => (
    miniFetch(resolve, reject, '/user/logout', {body: data})
);

// 获取项目源代码
module.exports.requestProject = (resolve, reject, projectId) => (
    miniFetch(resolve, reject, `/scratch/project/${projectId}`)
);

// 保存标题
module.exports.requestSaveProjectTitle = (resolve, reject, projectId, projectTitle) => {
    miniFetch(resolve, reject, '/scratch/saveProjcetTitle', {body:`id=${projectId}&title=${projectTitle}`})
};

// 保存缩略图
module.exports.requestSaveProjectThumbnail = (resolve, reject, projectId, thumbnailBlob) => {
    miniFetch(resolve, reject, `/scratch/thumbnail/${projectId}`, {body:thumbnailBlob, headers:{'Content-Type': 'image/png'}})
};

// 分享作品 或 取消分享
module.exports.requestShareProject = (projectId, s) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, `/scratch/shareProject/${projectId}`, {body:`s=${s}`});
});

// 获取课程卡数据
module.exports.requestCousreCard = (resolve, reject, lessonId) => {
    miniFetch(resolve, reject, `/course/getcard/${lessonId}`);
};


// 获取我的作品
module.exports.getMyProjectLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getMyProjectLibrary', {body: data});
});

// 获取优秀作品
module.exports.getYxProjectLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getYxProjectLibrary', {body: data});
});

// 获取背景
module.exports.getBackdropLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getBackdropLibrary', {body: data});
});
// 随机获取背景
module.exports.getRandomBackdrop = () => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getRandomBackdrop');
});

// 获取造型
module.exports.getCostumeLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getCostumeLibrary', {body: data});
});
// 随机获取造型
module.exports.getRandomCostume = () => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getRandomCostume');
});

// 获取声音
module.exports.getSoundLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getSoundLibrary', {body: data});
});
// 随机获取声音
module.exports.getRandomSound = () => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getRandomSound');
});

// 获取角色
module.exports.getSpriteLibrary = (data) => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getSpriteLibrary', {body: data});
});
// 随机获取角色
module.exports.getRandomSprite = () => new Promise((resolve, reject) => {
    miniFetch(resolve, reject, '/scratch/getRandomSprite');
});


// 公用函数
function miniFetch(resolve, reject, uri, params){
    // uri = "https://comecode.net"+uri;

    var opts = {
        headers:{
            'Accept':'application/json,text/plain,*/*',/* 格式限制：json、文本、其他格式 */
            'Content-Type':'application/x-www-form-urlencoded'/* 请求内容类型 */
        },
        method:'post'
    }
    if (params){
        if (params.headers) {opts['headers'] =  Object.assign(opts['headers'], params.headers)}
        if (params.method) {opts["method"] = params.method}
        if (params.body)   {opts["body"] = params.body}
    }

    fetch(uri, opts).then(response=>{
        var body = response.json();
        if(response.status == 200){
            return resolve(body);
        }
        return reject(body)
    })
    .catch(err=>reject(err))
};